package com.marisfrolg.adapter;

import java.util.ArrayList;
import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.beardedhen.androidbootstrap.BootstrapButton;
import com.lidroid.xutils.ViewUtils;
import com.marisfrolg.entity.HomeItem;
import com.marisfrolg.storage.HomeActivity;
import com.marisfrolg.storage.R;

public class GridViewHomeAdapter extends BaseAdapter {
	// 定义Context
	private HomeActivity activity;
	// 定义整型数组 即图片源
	private List<HomeItem> items=new ArrayList<HomeItem>(); 
	public GridViewHomeAdapter(HomeActivity c) {
		activity = c;
//		items.add(new HomeItem(HomeActivity.ACTIVITY_CHECK, "盘点", "fa-stack-overflow"));
//		items.add(new HomeItem(HomeActivity.ACTIVITY_PICKOUT, "拣配", "fa-external-link"));
//		items.add(new HomeItem(HomeActivity.ACTIVITY_MOVE, "移仓", "fa-retweet"));
//		items.add(new HomeItem(HomeActivity.ACTIVITY_UPSHELF, "上架", "fa-arrow-circle-up"));
//		items.add(new HomeItem(HomeActivity.ACTIVITY_BACKBIN, "归仓", "fa-sign-in"));
	}
	
	public void addItem(HomeItem item){
		items.add(item);
	}

	// 获取图片的个数
	public int getCount() {
		if(items==null){
			return 0;
		}
		return items.size();
	}

	// 获取图片在库中的位置
	public Object getItem(int position) {
		return position;
	}

	// 获取图片ID
	public long getItemId(int position) {
		return position;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {//容器控件调用这个方法来插入子控件
		LayoutInflater inflater=LayoutInflater.from(activity);
		View v=inflater.inflate(R.layout.home_item, null);//直接从布局文件转化为view对象
		BootstrapButton bt=(BootstrapButton)v.findViewById(R.id.bt);
		final HomeItem hi=items.get(position);
		bt.setText(hi.getDesc());
		bt.setLeftIcon(hi.getIcon());
		ViewUtils.inject(activity,v);
		bt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				activity.changeHandler.sendEmptyMessage(hi.getId());
			}
		});
		return v;
	}
}
